﻿namespace CamlQueryCreator.Common
{
    /// <summary>
    /// Оператор проверки на Null для CAML-запроса.
    /// </summary>
    public sealed class CamlOperatorIsNull : CamlOperator
    {
        private readonly string _fieldName;
        private readonly bool _isNullCheck;

        /// <summary>
        /// Оператор проверки на Null для CAML-запроса.
        /// <para>Конструктор.</para>
        /// </summary>
        internal CamlOperatorIsNull(CamlConnector parentBuilder, string fieldName, bool isNullCheck)
            : base(parentBuilder)
        {
            _fieldName = fieldName;
            _isNullCheck = isNullCheck;
        }

        /// <summary>
        /// Оператор проверки на Null для CAML-запроса.
        /// <para>Конструктор.</para>
        /// </summary>
        internal CamlOperatorIsNull(CamlConnector parentBuilder, IListFieldInfoBase fieldInfo, bool isNullCheck) : this(parentBuilder, fieldInfo.Name, isNullCheck)
        {
        }

        #region Overrides of CamlBase

        protected override string GetQuery(string childQuery)
        {            
            const string eqFormat = "<{1}><FieldRef Name='{0}'/></{1}>";
            var operationKind = _isNullCheck
                                    ? OperationKind.IsNull
                                    : OperationKind.IsNotNull;
            return string.Format(eqFormat, _fieldName, operationKind.ToName());
        }

        #endregion
    }
}
